<html>
	<head> <title> Umpires </title> </head>
	<body>
		<center><h1>Umpires</h1></center>
		<?php
			$db = mysql_connect('oniddb.cws.oregonstate.edu','rundej-db','Hp2tYI0CxEY4U9J9');
			if(mysql_errno($db)){
				echo mysql_errno($db);
			}
			mysql_select_db('rundej-db',$db);
			$tablequery = "select * from Umpires natural join Region";
			
			if($_POST["firstName"] && $_POST["lastName"] && $_POST["umpRegion"] && $_POST["umpID"])
			{
					$query = "select * from Region where Region_No = ".$_POST["umpRegion"];
					$result = mysql_query($query, $db);
					$num = mysql_num_rows($result);
					
					if($num > 0)
					{
						echo "Region found, adding umpire now</br>";
						$query = "INSERT INTO Umpires values (".$_POST["umpID"].",'".$_POST["firstName"]."','".$_POST["lastName"]."',".$_POST["matches"].",".$_POST["umpRegion"].")";
						mysql_query($query,$db);
						echo $query;
						$tablequery .= " where UNumber = ".$_POST["umpID"];
					}
					else
						echo "Region not found</br>";
				
			}
			if($_POST["umpIDup"])
			{
				if(!$_POST["newMatches"])
					$_POST["newMatches"] = 0;
				if($_POST["newRegion"])
				{
					$regions = mysql_query("Select * from Region where Region_No = ".$_POST["newRegion"],$db);
					$num = mysql_num_rows($regions);
					if($num == 0)
					{
						echo "Region not found";
						$_POST["newRegion"] = 0;
					}
				
				}
				if(!$_POST["newFirst"] || !$_POST["newLast"] || !$_POST["newRegion"])
				{
					$names = mysql_query("Select * from Umpires where UNumber = ".$_POST["umpIDup"],$db);
					$name = mysql_fetch_array($names);
					if(!$_POST["newFirst"])
						$_POST["newFirst"] = $name["UFname"];
					if(!$_POST["newLast"])
						$_POST["newLast"] = $name["ULname"];
					if(!$_POST["newRegion"])
						$_POST["newRegion"] = $name["Region_No"];
				}
				$query = "Update Umpires set UFname = '".$_POST["newFirst"]."', ULname = '".$_POST["newLast"]."', Matches_Umped = Matches_Umped + ".$_POST["newMatches"].", Region_No = ".$_POST["newRegion"]." where UNumber = ".$_POST["umpIDup"];
				mysql_query($query,$db);
				$tablequery .= " where UNumber = ".$_POST["umpIDup"];
			}
			
			$result = mysql_query($tablequery,$db);
			$numResults = mysql_num_rows($result);
		?>	
			<center><table border = "5">
				<tr>
					<th>Number</th>
					<th>Fname</th>
					<th>Lname</th>
					<th>Region Name</th>
					<th>Matches_Umped</th>
				</tr>
		<?php
			$c;
			$row;
			for($c = 0; $c < $numResults; $c++)
			{
				?><tr><?php
				$row = mysql_fetch_array($result);
				?><td><?php
				echo $row["UNumber"];
				?></td><td><?php
				echo $row["UFname"];
				?></td><td><?php
				echo $row["ULname"];
				?></td><td><?php
				echo $row["Region_Name"];
				?></td><td><?php
				echo $row["Matches_Umped"];
				?></td></tr><?php
			}
		?>
		</table></center>
		<center><a href = "umpires.php">Show all umpires</a>
		<center><h2>Add Umpire</h2></center>
		<center><form action = "umpires.php" method = "post">
				<table>
					<tr>
						<th>First Name: </th>
						<th>Last Name: </th>
						<th>Region: </th>
						<th>ID: </th>
						<th>Matches Umpired: </th>
					</tr>
					<tr>
						<td><input type = "text" name = "firstName"/></td><br>
						<td><input type = "text" name = "lastName"/></td><br>
						<td><input type = "text" name = "umpRegion"/></td><br>
						<td><input type = "text" name = "umpID"/></td><br>
						<td><input type = "text" name = "matches"/></td><br>
					</tr>
					<tr>			
						<td></td>
						<td></td>
						<td></td>
						<td></td>
						<td><input type = "submit" value = "Add Umpire"/></td>
					</tr>
				</table>
			</form></center>
		<center><h2>Update Umpire Data</h2></center>

		<form action = "umpires.php" method = "post">
			<center><table>
				<tr>
					<th>ID of Umpire to Update: </th>
					<td><input type = "text" name = "umpIDup"></td>
				</tr>
				<tr>
					<th>First Name: </th>
					<th>Last Name: </th>
					<th>Region: </th>
					<th>Matches Umpired: </th>
				</tr>
				<tr>
					<td><input type = "text" name = "newFirst"/></td><br>
					<td><input type = "text" name = "newLast"/></td><br>
					<td><input type = "text" name = "newRegion"/></td><br>
					<td><input type = "text" name = "newMatches"/></td><br>
				</tr>
				<tr>
					<td></td>
					<td></td>
					<td></td>
					<td><input type = "submit" name = "updateUmp" value = "Update Umpire"/></td>
				</tr>
			</table>

		</form>
		</br></br></br>
		<center>
			<a href = "index.php">Home</a>	
			<a href = "regions.php">Regions</a>	
			<a href = "players.php">Players</a>
		</center>	
	</body>
</html>
			
